package com.drojian.demo1;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.drojian.demo.R;
import com.drojian.demo1.root_fragment.NineDrawableFragment;
import com.drojian.demo1.root_fragment.RootFragment1;
import com.drojian.demo1.root_fragment.RootFragment2;
import com.drojian.demo1.root_fragment.RootFragment3;
import com.drojian.demo1.root_fragment.RootFragment4;
import com.drojian.demo1.root_fragment.RootFragment5;
import com.drojian.demo1.root_fragment.RootFragment6;
import com.drojian.demo4.CommunicationFragment;
import com.drojian.root.myFragmentTitleAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;


public class BaseFragment1 extends Fragment {
    private ViewPager2 viewPager2;
    private TabLayout tabLayout;
    private List<Fragment> myFragmentList;
    private List<String> myTitleList;
    private myFragmentTitleAdapter myTitleAdapter;

    public static BaseFragment1 newInstance() {
        return new BaseFragment1();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base1, container, false);

        // 初始化ViewPager2和TabLayout
        viewPager2 = view.findViewById(R.id.first_viewPage);
        tabLayout = view.findViewById(R.id.top_tableLayout);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        myTitleAdapter = new myFragmentTitleAdapter(getChildFragmentManager(), this.getLifecycle(),myFragmentList, myTitleList);
        viewPager2.setAdapter(myTitleAdapter);


        // 使用TabLayoutMediator关联ViewPager2和TabLayout
        new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
            String title = myTitleAdapter.getTabTitle(position);
            if (title != null) {
                tab.setText(title);
            }
        }).attach();
    }

    private void initData() {
        myFragmentList = new ArrayList<>();
        myFragmentList.add(RootFragment1.newInstance());
        myFragmentList.add(RootFragment2.newInstance());
        myFragmentList.add(RootFragment3.newInstance());
        myFragmentList.add(RootFragment4.newInstance());
        myFragmentList.add(RootFragment5.newInstance());
        myFragmentList.add(RootFragment6.newInstance());
        myFragmentList.add(CommunicationFragment.newInstance());
        myFragmentList.add(NineDrawableFragment.newInstance());

        myTitleList = new ArrayList<>();
        myTitleList.add("第一组");
        myTitleList.add("第二组");
        myTitleList.add("第三组");
        myTitleList.add("第四组");
        myTitleList.add("第五组");
        myTitleList.add("第六组");
        myTitleList.add("通信");
        myTitleList.add("点9图片");
    }
}